<?php

/**
 * block_guestbook
 * 
 * @author Kirill Krasnoschekov
 */
class block_guestbook extends block_base {
    
    // Guestbook entity
    private $gb;
    // Presentation class
    private $pr;

    /**
     * Initialize block
     * 
     * @author Kirill Krasnoschekov
     */
    public function init() {
        if ( !isset($this->gb) ) {
            global $DB;
            ////Including libs
            require_once 'lib/lib.php';
            require_once 'lib/lib.xmldb.php';
            //Initializing the guestbook
            $this->gb = new XMLDBGuestbook($DB);
            $this->gb->SetMessagesPerPage(8);
            ////Setting the title
            $this->title = get_class($this->gb);
        }
    }

    /**
     * Handle something just after instance data loaded
     * 
     * @author Kirill Krasnoschekov
     */
    public function specialisation() {}
    
    /**
     * Get the content
     * 
     * @author Kirill Krasnoschekov
     */
    public function get_content() {
        if ($this->content !== NULL) {
            //Content is defined
            return $this->content;
        }
        //Define new content
        $this->content = new stdClass;
        
        ////Timer
        $timeStart = microtime();
        
        ////Library
        require_once 'lib/logic.php';
        
        ////Debug mode
        define('DEBUG', True);
        
        ////Forms
        require_once 'guestbook_form.php';
        $msg_add_form = new guestbook_message_add_form;
        $msg_update_form = new guestbook_message_update_form;
        $cmt_form = new guestbook_comment_form;
        
        ////Input
        //Fetching data
        if ( $msg_add_data = $msg_add_form->get_data() ) {
            //Message add
            $data = (array) $msg_add_data;
        }elseif ( $msg_update_data = $msg_update_form->get_data() ) {
            //Message updage
            $data = (array) $msg_update_data;
        }elseif ( $cmt_data = $cmt_form->get_data() ) {
            //Comment
            $data = (array) $cmt_data;
        }else{
            //Something else through hyperlink (and hence GET)
            $data = $_GET;
        }

        //Handling data
        list($posted_message, $posted_comment) = HandleData($data, $this->gb);
        
        //Clearing forms
        $msg_add_form->reset_submission();        
        $msg_update_form->reset_submission();
        $cmt_form->reset_submission();
        
        //Total pages
        $pages = $this->gb->CountPages();
        //Selected page
        if ( isset($data['page']) ) {
            $page = $data['page'];
            if ( $page > $pages - 1 ) {
                $page = 0;
            }
        }else{
            $page = 0;
        }
        
        
        ////Output
        //Templates
        require_once 'lib/tmpl.moodle.php';
        //Global vars
        $globalVars = array('title' => $this->title, 
                            'page' => $page, 
                            'pages' => $pages);
        //Presentation
        require_once 'lib/presentation.moodle.php';
        $this->pr = new MoodlePresentation
                ($tmpl, $globalVars, $this->content, 
                $msg_add_form, $msg_update_form, $cmt_form);
        Display($this->pr, $this->gb, 
                $posted_message, $posted_comment, $page, $timeStart);
        
        return $this->content;
    }
}